package com.badlogic.androidgames;

//import twitter4j.Query;
//import twitter4j.QueryResult;
//import twitter4j.Tweet;
//import twitter4j.Twitter;
//import twitter4j.TwitterException;
//import twitter4j.TwitterFactory;

import com.badlogic.androidgames.asyncutil.AsyncSearching;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.Settings.GameDifficult;
import com.badlogic.androidgames.util.*;
import com.badlogic.androidgames.util.Result.ResourceType;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class DialogHandler implements OnClickListener, OnItemSelectedListener, OnItemClickListener{

	GLGame g;
	Result result[];
	Bitmap bitmap[]; 
	AsyncSearching downloader;
	
	public String ricercaS;
	public long idRicercaS; 
	public int pageS;

	public DialogHandler(GLGame g) {
		this.g = g;
	}
	

	@Override
	public void onClick(View v) {

		if(v.getId() == R.id.button1)
		{

			InputMethodManager  m;
			m = (InputMethodManager) g.getApplicationContext().getSystemService("input_method");
			m.hideSoftInputFromWindow(v.getWindowToken(), 0);

			EditText editText1;
			editText1 = (EditText)g.myDialog.findViewById(R.id.editText1);
			
			ricercaS = editText1.getText().toString();

			Spinner spinner = (Spinner) g.myDialog.findViewById(R.id.spinner1);
			idRicercaS = spinner.getSelectedItemId();
			
			//gestione ricerca su flickr
			//ricerca twitter
			//gestione ricerca su youtube
			pageS = 1;
			downloader = (AsyncSearching) new AsyncSearching(g, ricercaS, pageS).execute((int)idRicercaS);
			
		}
		else if(v.getId() == R.id.buttonmore)
		{
			pageS++;
			downloader = (AsyncSearching) new AsyncSearching(g, ricercaS, pageS ).execute((int)idRicercaS);
		}
		else if(v.getId() == R.id.buttonprev)
		{
			pageS--;
			downloader = (AsyncSearching) new AsyncSearching(g, ricercaS, pageS ).execute((int)idRicercaS);
		}
		else if(v.getId() == R.id.buttonback)
		{
			//g.myDialog.hide();

			
			//g.myDialog.getWindow();
			g.myDialog.setContentView(R.layout.ricerca);
			Spinner spinner = (Spinner) g.myDialog.findViewById(R.id.spinner1);
			// Create an ArrayAdapter using the string array and a default spinner layout
			ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(g,
					R.array.sn_array, android.R.layout.simple_spinner_item);
			// Specify the layout to use when the list of choices appears
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			// Apply the adapter to the spinner
			spinner.setAdapter(adapter);
			spinner.setOnItemSelectedListener(g.myDialogHandler);

			Button button = (Button)g.myDialog.findViewById(R.id.button1);
			button.setOnClickListener(g.myDialogHandler);
			
			//g.myDialog.show();
		}
		else if(v.getId() == R.id.buttonRicercaError)
		{
			g.myDialog.setContentView(R.layout.ricerca);
			Spinner spinner = (Spinner) g.myDialog.findViewById(R.id.spinner1);
			ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(g,
					R.array.sn_array, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			spinner.setOnItemSelectedListener(g.myDialogHandler);
			Button button = (Button)g.myDialog.findViewById(R.id.button1);
			button.setOnClickListener(g.myDialogHandler);
		}
		else if(v.getId() == R.id.buttonDifficultOk)
		{
			Spinner spinner = (Spinner) g.myDialog.findViewById(R.id.spinnerDifficult);
			int it = spinner.getSelectedItemPosition();
			Settings.gameDifficult = GameDifficult.values()[it];
			g.myDialog.hideAndResume();
		}
		else if(v.getId() == R.id.buttonExitNo)
		{
			g.myDialog.hideAndResume();
		}
		else if(v.getId() == R.id.buttonExitYes)
		{
			g.setScreen(new MainMenuScreen(g));
			g.myDialog.hideAndResume();
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {

		// An item was selected. You can retrieve the selected item using
		//Spinner spinner = (Spinner) g.myDialog.findViewById(R.id.spinner1);
		//spinner.setMinimumWidth(200);
		//spinner.setSelection(pos);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
	{	
		//String url = "vnd.youtube://"+result[(int)arg3].getId();
		//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));//"vnd.youtube://YoXdA4siPdQ"
		//g.startActivity(intent);
		
		result = downloader.result;
		//bitmap = downloader.bitmap;

		if(result[(int)arg3].getType() == ResourceType.YOUTUBE)
		{
			((GameScreen)g.getCurrentScreen()).tbToFill.setUrlStr("http://i.ytimg.com/vi/"+result[(int)arg3].getId()+"/default.jpg");
			((GameScreen)g.getCurrentScreen()).tbToFill.toLoad = true;
			((GameScreen)g.getCurrentScreen()).tbToFill.resource = result[(int)arg3];
			((GameScreen)g.getCurrentScreen()).tbToFill.complete = true;
			g.myDialog.hideAndResume();
		}
		else if(result[(int)arg3].getType() == ResourceType.TWITTER)
		{	
			((GameScreen)g.getCurrentScreen()).tbToFill.textContent = result[(int)arg3].getText();
			((GameScreen)g.getCurrentScreen()).tbToFill.resource = result[(int)arg3];
			((GameScreen)g.getCurrentScreen()).tbToFill.complete = true;
			g.myDialog.hideAndResume();
		}
		else if(result[(int)arg3].getType() == ResourceType.FLICKR)
		{	
			((GameScreen)g.getCurrentScreen()).tbToFill.setUrlStr(result[(int)arg3].getUrlImg());
			((GameScreen)g.getCurrentScreen()).tbToFill.toLoad = true;
			((GameScreen)g.getCurrentScreen()).tbToFill.resource = result[(int)arg3];
			((GameScreen)g.getCurrentScreen()).tbToFill.complete = true;
			g.myDialog.hideAndResume();
		}
		
		if(((GameScreen)g.getCurrentScreen()).tbToFill.text.isEmpty())
			((GameScreen)g.getCurrentScreen()).tbToFill.userCompletation = false;
		else 
			((GameScreen)g.getCurrentScreen()).tbToFill.userCompletation = true;
	}
}
